a = []

for i in range(5):
    a2 = input("请输入商品的编号: ")
    a3 = input("请输入商品的名称: ")
    a.append((a2, a3))

print("\n商品列表:")
for a1 in a:
    print(f"{a1[0]} {a1[1]}")

b = []

while True:
    d = input("请输入要购买的商品编号: ")
    if d == 'q':
        break
    c = False
    for a1 in a:
        if a1[0] == d:
            b.append(a1)
            print(f"商品已成功添加到购物车: {a1[1]}")
            c = True
            break
    if not c:
        print("该商品不存在！")

print("\n您选择的商品为：")
for a1 in reversed(b):
    print(f"{a1[0]} {a1[1]}")